package com.project.controller.web;

import com.alibaba.fastjson.JSONObject;
import com.project.entity.JobApply;
import com.project.entity.University;
import com.project.myBeans.ResponseBean;
import com.project.service.AreaService;
import com.project.service.UniversityService;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by WheelChen on 2017/6/22.
 * <p>
 * 大学相关操作
 */
@Controller
@RequestMapping("/university")
public class UniversityController extends BaseService {
    @Autowired
    UniversityService universityService;

    @Autowired
    private AreaService areaService;


    /**
     * author: WheelChen
     * date: 2017-6-22
     * function: 查找所有学院
     *
     * @param
     * @return {json数组} university
     */
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    @ResponseBody
    public ResponseBean findAll() {
        ResponseBean responseBean = new ResponseBean();
        try {
            List<University> universities;
            universities = universityService.findAll();
            responseBean.setSuccess(true);
            responseBean.setData(universities);
            return responseBean;
        } catch (Exception e) {
            responseBean.setSuccess(false);
            return responseBean;
        }
    }

    @RequestMapping(value = "/addOne", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/7/11
     * function: 添加一个大学
     *
     * @param university
     * @return
     */
    public ResponseBean addOne(@RequestBody University university) {
        ResponseBean responseBean = new ResponseBean();
        try {
            processSuccessDeleteUpdate(universityService.add(university), responseBean);
            responseBean.setData(university.getId());
            return responseBean;
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
            return responseBean;
        }
    }

    @RequestMapping(value = "/findUniversityAndSchoolsByCondition", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/9/15
     * function: 获取大学及大学下的学院和对应的学院管理员信息（根据学院管理员数量分页）
     *
     * @param university
     * @return
     */
    public ResponseBean findUniversityAndSchoolsByCondition(@RequestBody University university) {
        ResponseBean responseBean = new ResponseBean();
        List<JSONObject> jsonObjectList = new ArrayList<>();
        try {
            if (university.getPageIndex() > 0) {
                university.setPageIndex(university.getPageIndex() - 1);
            }
            List<University> universityList = universityService.findUniversityAndSchoolsByCondition(university);
            for (University university1: universityList){
                JSONObject jsonObject = new JSONObject();
                //大学表信息
                jsonObject.put("Institutions", university1);
                //区域信息 省/市/区
                if (university1.getLocationid() != null)
                    jsonObject.put("area", areaService.findOneDetailById(university1.getLocationid()));
                jsonObjectList.add(jsonObject);
            }
            responseBean.setData(jsonObjectList);
            responseBean.setTotal(university.getTotal());
            responseBean.setSuccess(true);
            responseBean.setMessage("查找成功");
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }

    @RequestMapping(value = "/findUniversityAndSchoolsByCondition2", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/9/20
     * function: 查找所有学校信息及其下属学院信息 + 条件查找（根据大学数量分页）
     *
     * @param university
     * @return
     */
    public ResponseBean findUniversityAndSchoolsByCondition2(@RequestBody University university){
        ResponseBean responseBean = new ResponseBean();
        try {
            if (university.getPageIndex() > 0) {
                university.setPageIndex(university.getPageIndex() - 1);
            }
            responseBean.setData(universityService.findUniversityAndSchoolsByCondition2(university));
            responseBean.setTotal(university.getTotal());
            responseBean.setSuccess(true);
            responseBean.setMessage("获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }

    @RequestMapping(value = "/findByCondition", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/9/18
     * function: 模糊分页搜索仅查学校信息（包含学校地理位置）
     *
     * @param university
     * @return
     */
    public ResponseBean findByCondition(@RequestBody University university) {
        ResponseBean responseBean = new ResponseBean();
        List<JSONObject> jsonObjectList = new ArrayList<>();
        try {
            if (university.getPageIndex() > 0) university.setPageIndex(university.getPageIndex() - 1);
            List<University> universityList = universityService.findByCondition(university);
            for (University university1: universityList){
                JSONObject jsonObject = new JSONObject();
                //大学表信息
                jsonObject.put("jsonObject", university1);
                //区域信息 省/市/区
                if (university1.getLocationid() != null)
                jsonObject.put("area", areaService.findOneDetailById(university1.getLocationid()));
                jsonObjectList.add(jsonObject);
            }
            responseBean.setData(jsonObjectList);
            responseBean.setTotal(university.getTotal());
            responseBean.setSuccess(true);
            responseBean.setMessage("获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }

}
